package com.lbs.jiepiaisi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.adapter.AlarmListAdapter;
import com.lbs.jiepiaisi.base.BaseActivity;
import com.lbs.jiepiaisi.base.ErrorException;
import com.lbs.jiepiaisi.base.ProgressObserver;
import com.lbs.jiepiaisi.bean.AlarmBean;
import com.lbs.jiepiaisi.config.Api;
import com.lbs.jiepiaisi.utils.RxSchedulers;
import com.lbs.jiepiaisi.view.CustomLoadMoreView;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 时间：2017/10/19 10:19
 * 作用：单个设备报警信息
 */
public class AlarmListActivity extends BaseActivity {
    private RecyclerView recyclerView;
    private SwipeRefreshLayout refreshLayout;
    private AlarmListAdapter mAdapter;
    List<AlarmBean.Result.Item> listItem = new ArrayList<>();
    String eqNo;//设备编码
    String deptId;//部门编码
    String ownerName;//姓名;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isTemplate = true;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_list);

        getExras();
        findView();
        setListener();
        refresh();
    }

    public void getExras() {
        Intent intent = getIntent();
        eqNo = intent.getStringExtra("eqNo");
        deptId = intent.getStringExtra("deptId");
        ownerName = intent.getStringExtra("ownerName");
        if("".equals(ownerName)) {
            titleView.setText("告警");
        }else {
            titleView.setText("告警-" + ownerName);
        }
    }

    private void showEmpty() {
        mAdapter.setEmptyView(R.layout.layout_empty);
    }

    public void refresh() {
        Api.getInstance().createService()
                .loadAlarmList(deptId,size,page,eqNo)
                .compose(RxSchedulers.observableIO2Main(this))
                .subscribe(new ProgressObserver<AlarmBean>(this,true){
                    @Override
                    public void onSuccess(AlarmBean result) {
                        refreshLayout.setRefreshing(false);
                        List data = result.getResult().getItems();
                        if (page == 1) {
                            if (data.size() == 0) showEmpty();
                            listItem.clear();
                            listItem.addAll(data);
                            mAdapter.setNewData(listItem);
                        } else {
                            mAdapter.addData(data);
                        }

                        if (data.size() < size) {
                            mAdapter.loadMoreEnd();
                        } else {
                            mAdapter.loadMoreComplete();
                        }
                        page++;
                    }

                    @Override
                    public void onFailure(ErrorException e) {
                        refreshLayout.setRefreshing(false);
                        mAdapter.loadMoreFail();
                        showEmpty();
                        Log.e("eeeee","告警错误 = "+e.getCode());
                    }
                });
    }

    public void findView() {
        recyclerView = findViewById(R.id.recyclerView);
        refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setColorSchemeResources(R.color.blue);

        mAdapter = new AlarmListAdapter(R.layout.list_item_platform_warning, listItem);
        mAdapter.bindToRecyclerView(recyclerView);
        mAdapter.disableLoadMoreIfNotFullPage();
        mAdapter.setLoadMoreView(new CustomLoadMoreView());

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter);
        mAdapter.setEnableLoadMore(true);
    }

    public void setListener() {
        refreshLayout.setOnRefreshListener(() -> {
            mAdapter.setEnableLoadMore(false);
            page = 1;
            refresh();
        });

        mAdapter.setOnLoadMoreListener(()-> {
            refresh();
        }, recyclerView);
    }
}
